package restaurant;
import java.util.Iterator;
import java.util.Vector;

public class ListeRestaurant {

	// Produits du menu
	private Vector <Restaurant> restaurants;
	
	// Instance liste restaurant
	static private ListeRestaurant instance = null;
	
	public ListeRestaurant(){
		
		restaurants = new Vector <Restaurant> ();
	}
	
	// Retourne l'instance unique de la classe
	static public ListeRestaurant getInstance(){
		
		if (instance == null) instance = new ListeRestaurant();
		
		return instance;
	}
	
	// Ajouter un restaurant
	public void ajouterRestaurant(Restaurant restaurant){
		
		restaurants.add(restaurant);
	}
	
	// Supprimer un restaurant
	public void supprimerRestaurant(Restaurant restaurant){
		
		restaurants.remove(restaurant);
	}
	
	// Supprimer un restaurant
	public void supprimerRestaurant(int index) throws Exception{
		
		try{
			restaurants.remove(index);
		}
		
		catch(Exception e){
			
			throw new Exception(" ********** Restaurant non supprime, index invalide ("+index+") ********** ");
		}
	}
	
	// Retourne un restaurant
	public Restaurant getRestaurant(int index) throws Exception{
		
		Restaurant resto = null;
		
		try{
			resto = restaurants.get(index);
		}
		
		catch(Exception e){
			
			throw new Exception(" ********** Restaurant introuvable, index invalide ("+index+") ********** ");
		}
		
		return resto;
		
	}
	
	// Retourne un restaurant
	public Restaurant trouverRestaurant(int idResto){
		
		Restaurant resto = null;
		
		for(int i = 0; i < restaurants.size(); i++){
			
			if (restaurants.get(i).getIdResto() == idResto){
				resto = restaurants.get(i);
			}
		}
		
		return resto;
	}
	
	// Retourne la liste des restaurants
	public Vector <Restaurant> getListe(){
		return restaurants;
	}
	
	// Liste des restaurants
	public String toString(){
		
		String resto = new String ();
		Iterator <Restaurant> ite = restaurants.iterator();
		
		while(ite.hasNext()) resto = resto + ite.next().toString();
		
		return resto;
	}
}
